//
//  JCBusinessCardContentView.swift
//  JChat
//
//  Created by JIGUANG on 2017/8/31.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory

class JCRedPackContentView: UIView, JCMessageContentViewType {

    
    private weak var _delegate: JCMessageDelegate?
    private var _message: JCMessageType!
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        _commonInit()
    }
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _commonInit()
    }
    
    open func apply(_ message: JCMessageType) {
        guard let content = message.content as? JCRedPackContent else {
            return
        }
        
        redpackStatusLabel.text = ""
        var status: MTOpenRedpackStatus = MTOpenRedpackStatus.unkonw
        if let msgId = message.jmessage?.serverMessageId {
            if let info = MTRedpacketInfoDB.shareInstance.quaryData(msgId: msgId).first {
                var text = ""
                status = info.status
                switch info.status {
                case .clearance:
                    text = "红包已抢完"
                case .invalid:
                    text = "红包已过期"
                case .opened:
                    text = "红包已领取"
                default:
                    text = ""
                    break
                }
                redpackStatusLabel.text = text
            }
        }
        //let info = MTRedpacketInfoDB.shareInstance
        
        _extras = content.extras
        _message = message
        _delegate = content.delegate
        _isSelf = (message.sender?.username == JMSGUser.myInfo().username)
        
        redpackTitle.text = _extras?["message"] as? String
        // 其值是 "user"。如果是 App 管理员下发的消息，是 "admin"
        if let conversationType: Int = _extras?["targetType"] as? Int {
            //redpacketTypeLabel.text = conversationType == 1 ? "个人红包" : conversationType == 2 ? "俱乐部红包" : "广场红包"
            redpacketTypeLabel.text = conversationType == 1 ? "福利红包" : conversationType == 2 ? "福利红包" : "广场红包"
        }
        
        let isSelf: Bool = (message.sender?.username == JMSGUser.myInfo().username)
        if isSelf {
            if (status == MTOpenRedpackStatus.opened || status == MTOpenRedpackStatus.clearance || status == MTOpenRedpackStatus.invalid) {
                backgroudV.image = UIImage(named: "chat_bubble_send_nor_disable")
            } else {
                backgroudV.image = UIImage(named: "chat_bubble_send_nor")
            }
        } else {
            if (status == MTOpenRedpackStatus.opened || status == MTOpenRedpackStatus.clearance || status == MTOpenRedpackStatus.invalid) {
                backgroudV.image = UIImage(named: "chat_bubble_recive_disable")
            } else {
                backgroudV.image = UIImage(named: "chat_bubble_recive_highlighted")
            }
        }
        
        //backgroudV.image = UIImage(named: isSelf ? "chat_bubble_send_nor" :  "chat_bubble_recive_highlighted")
        if !isSelf {
            redpackImageView.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(24)
            }
            
            redpacketTypeLabel.snp.makeConstraints { (make) in
                make.left.equalTo(redpackImageView)
                make.top.equalTo(lineView.snp.bottom).offset(5)
            }
            
            lineView.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(11)
                make.right.equalToSuperview().offset(-4)
            }
        }

    }
    
    lazy var backgroudV: UIImageView = {
        let v = UIImageView(image: UIImage(named: "chat_bubble_send_nor"))
        return v
    }()
    
    //UIImage(named: "chat_bubble_send_nor")
    lazy var topTipView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.clear
        return v
    }()
    
    lazy var redpackImageView: UIImageView = {
        let v = UIImageView(image: UIImage(named: "club_redpacket_new"))
        v.contentMode = .scaleAspectFill
        v.sizeToFit()
        return v
    }()
    
    lazy var redpackTitle: UILabel = {
        let title: UILabel = UILabel()
        title.font = PingFangSCMediumFont(16)
        title.textColor = UIColor.white
        title.adjustsFontSizeToFitWidth = true
        return title
    }()
    
    lazy var redpackStatusLabel: UILabel = {
        let title: UILabel = UILabel()
        title.font = PingFangSCMediumFont(12)
        title.textColor = UIColor(hex: 0x9b9b9b)
        return title
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xf6f6f6)
        return v
    }()
    
    lazy var redpacketTypeLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(10)
        l.textColor = UIColor(hex: 0x29282b)
        return l
    }()
    
    private var _extras: [String : Any]?
    /// 是否是自己发送的消息
    private var _isSelf: Bool?
    
    private func _commonInit() {
        _tapGesture()

        addSubview(backgroudV)
        backgroudV.addSubview(topTipView)
        topTipView.addSubview(redpackTitle)
        topTipView.addSubview(redpackImageView)
        topTipView.addSubview(redpackStatusLabel)
        addSubview(lineView)
        addSubview(redpacketTypeLabel)
        
        backgroudV.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        topTipView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(55)
        }
        
        redpackTitle.snp.makeConstraints { (make) in
            //make.edges.equalTo(UIEdgeInsets(top: 16, left: 9, bottom: 16, right: 9))
            make.right.equalToSuperview().offset(-29)
            make.top.equalTo(redpackImageView)
            make.left.equalTo(redpackImageView.snp.right).offset(8)
            
        }
        
        redpackImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(10)
            make.width.equalTo(38)
            make.height.equalTo(44)
        }
        
        redpackStatusLabel.snp.makeConstraints { (make) in
            make.left.equalTo(redpackTitle)
            make.top.equalTo(redpackTitle.snp.bottom)
            make.right.equalTo(redpackTitle)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(2)
            make.top.equalTo(redpackImageView.snp.bottom).offset(5)
        }
        
        redpacketTypeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(redpackImageView)
            make.top.equalTo(lineView.snp.bottom).offset(5)
        }
        
    }
    
    func _tapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(_clickCell))
        tap.numberOfTapsRequired = 1
        addGestureRecognizer(tap)
    }
    
    @objc func _clickCell() {
        _delegate?.tapRedPack?(message: _message)
    }

}
